package com.colorful.insuranceproject.mine;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.base.TitleActivity;
import com.colorful.insuranceproject.bean.MoneyInfo;
import com.colorful.insuranceproject.bean.PersonInfoBean;
import com.colorful.insuranceproject.commonActivity.CheckingInformationActivity;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.microDrug.PayPwdBindActivity;
import com.colorful.insuranceproject.request.MineRequest;
import com.lovely3x.common.managements.user.UserManager;
import com.lovely3x.common.utils.Response;
import com.lovely3x.loginandresgiter.bean.SimpleUser;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by handsome-Bob on 2017/8/21.
 * <p>
 * 额度信息
 */
public class QuotaInfoActivity extends TitleActivity {

    private static final int GET_QUOTA_URL = 0X1;

    public static final String EXTRA_QUOTA_INFO = "extra_quota_info";

    private static final int TEXT_TRANSPORT = 0X2;

    MineRequest mineRequest;

    /**
     * 投保总额
     */
    @Bind(R.id.tv_total_insurance_amount)
    TextView tvTotalInsuranceAmount;

    /**
     * 当前额度
     */
    @Bind(R.id.tv_current_quota)
    TextView tvCurrentQuota;

    /**
     * 冻结额度
     */
    @Bind(R.id.tv_frozen_quota)
    TextView tvFrozenQuota;

    /**
     * 投保期限
     */
    @Bind(R.id.tv_insurance_period)
    TextView tvInsurancePeriod;

    MoneyInfo mi;

    @Override
    protected int getContentView() {
        return R.layout.activity_quota_info;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setTitle(getString(R.string.quota_information));
    }

    @Override
    protected void onViewInitialized() {
        mineRequest = new MineRequest(getHandler());
        if (mi != null) {
            tvTotalInsuranceAmount.setText(mi.getInsuranceMoney());//投保总额
            tvCurrentQuota.setText(mi.getCurrentMoney());//当前额度
            tvFrozenQuota.setText(mi.getLockMoney());//冻结额度
            tvInsurancePeriod.setText(mi.getInsuranceTime());//投保期限
        } else {
            mineRequest.getUserInfoUrl(GET_QUOTA_URL);
        }
    }

    @Override
    protected void onInitExtras(@NonNull Bundle bundle) {
        super.onInitExtras(bundle);
        mi = bundle.getParcelable(EXTRA_QUOTA_INFO);
    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        switch (msg.what) {
            case GET_QUOTA_URL:
                if (response.isSuccessful) {
                    setPageData((PersonInfoBean) response.obj);
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;
        }
    }

    private void setPageData(PersonInfoBean obj) {
        mi = obj.getMoneyInfo();
        tvTotalInsuranceAmount.setText(mi.getInsuranceMoney());//投保总额
        tvCurrentQuota.setText(mi.getCurrentMoney());//当前额度
        tvFrozenQuota.setText(mi.getLockMoney());//冻结额度
        tvInsurancePeriod.setText(mi.getInsuranceTime());//投保期限
    }

    @OnClick({R.id.tv_payment_code, R.id.tv_collection_code, R.id.success, R.id.fail})
    void OnClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_payment_code:
                launchActivityNeedLogin(PaymentCodeActivity.class);
                break;
            case R.id.tv_collection_code://  修改支付密码
                SimpleUser simpleUser = (SimpleUser) UserManager.getInstance().getCurrentUser();
                if (simpleUser.getPayPwd().equals("") || simpleUser.getPayPwd().equals("null")) {
                    launchActivity(CheckingInformationActivity.class);
                } else {
                    launchActivity(PayPwdBindActivity.class);
                }
                break;
            case R.id.success:
                mineRequest.textTransportSuccess("1", "1", TEXT_TRANSPORT);
                break;
            case R.id.fail:
                mineRequest.textTransportSuccess("2", "2", TEXT_TRANSPORT);
                break;
        }
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }
}
